package DAOImp;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import DAO.AbsHbnDao;
import DAO.ProductDetailDAO;
import POJO.ProductDetail;

@Repository
public class ProductDetailDaoImp extends AbsHbnDao<ProductDetail> implements ProductDetailDAO{

	static final String FIND_PRODUCT_DISCOUNT = "FROM ProductDetail P WHERE P.oldPrice > P.price ORDER BY P.oldPrice - P.price DESC";
	static final String FIND_PRODUCT_NEW = "FROM ProductDetail P ORDER BY P.product.dateCreate DESC";
	static final String GET_COUNT_IN_STORE = "SELECT sum(s.count) FROM StoreProductDetail s  WHERE s.pk.productDetail.productDetailId = :id";
	static final String GET_PRODUCT_DETAILS = "FROM ProductDetail p WHERE p.product.productId = :id";
	
	@SuppressWarnings("unchecked")
	public List<ProductDetail> getProductDiscount(int count) {
		return getSession().createQuery(FIND_PRODUCT_DISCOUNT).setMaxResults(count).list();
	}

	public Long getCountInStore(int id) {
		return (Long) getSession().createQuery(GET_COUNT_IN_STORE).setParameter("id", id).uniqueResult();
	}

	@Override
	public ProductDetail get(Serializable id) {
		return (ProductDetail) getSession().createQuery("FROM ProductDetail d WHERE d.productDetailId = :id").setParameter("id", id).list().get(0);
	}

	@SuppressWarnings("unchecked")
	public List<ProductDetail> getProductNew(int count) {
		return getSession().createQuery(FIND_PRODUCT_NEW).setMaxResults(count).list();
	}

	@SuppressWarnings("unchecked")
	public List<ProductDetail> getProductDetailsById(int id) {
		return getSession().createQuery(GET_PRODUCT_DETAILS).setParameter("id", id).list();
	}
}
